package com.starling.extend.core
{
	import feathers.controls.Button;
	import feathers.controls.Check;
	import feathers.controls.IScrollBar;
	import feathers.controls.ImageLoader;
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.NumericStepper;
	import feathers.controls.PageIndicator;
	import feathers.controls.PickerList;
	import feathers.controls.ProgressBar;
	import feathers.controls.Radio;
	import feathers.controls.ScrollBar;
	import feathers.controls.ScrollText;
	import feathers.controls.Scroller;
	import feathers.controls.Slider;
	import feathers.controls.TabBar;
	import feathers.controls.TextArea;
	import feathers.controls.TextInput;
	import feathers.controls.ToggleSwitch;
	import feathers.controls.popups.DropDownPopUpContentManager;
	import feathers.controls.popups.VerticalCenteredPopUpContentManager;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.controls.text.TextFieldTextEditor;
	import feathers.controls.text.TextFieldTextRenderer;
	import feathers.core.ITextRenderer;
	import feathers.core.ToggleGroup;
	import feathers.data.ListCollection;
	import feathers.display.Scale9Image;
	import feathers.skins.SmartDisplayObjectStateValueSelector;
	import feathers.text.BitmapFontTextFormat;
	
	import flash.text.TextFormat;
	
	import starling.display.DisplayObject;
	import starling.display.DisplayObjectContainer;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.text.BitmapFont;
	import starling.textures.Texture;
	import starling.utils.AssetManager;

	internal class ComponentDecoder
	{
		
		/**
		 * 
		 * @param info
		 * @param packet
		 * @return 
		 * 
		 */				
		public static function createScrollText(info:Object ,packet:PacketDecoder):ScrollText
		{
			var st:ScrollText = new ScrollText();
			
			var ttf:TextFormat = new TextFormat();
			ttf.font = info.textInfo.font;
			ttf.size = info.textInfo.size;
			ttf.align = info.textInfo.align;
			ttf.color = info.textInfo.color;
			ttf.underline = info.textInfo.underline;
			ttf.bold = info.textInfo.bold;
			ttf.italic = info.textInfo.italic;
			
			st.textFormat = ttf;
			st.horizontalScrollBarFactory = st.verticalScrollBarFactory = function():IScrollBar
			{
				var scrollBar:ScrollBar = new ScrollBar();
				scrollBar.direction = ScrollBar.DIRECTION_VERTICAL;
				scrollBar.trackLayoutMode = ScrollBar.TRACK_LAYOUT_MODE_SINGLE;
				scrollBar.thumbFactory = function():Button
				{
					var track1:Button = new Button();
					track1.defaultSkin = packet.createScale9Image(info.scrollbar);
					track1.downSkin = packet.createScale9Image(info.scrollbar);
					return track1;
				};
				
				return scrollBar;
			}
			
			return st;
		}
		
		
		
		
		/**
		 *  
		 * @param info
		 * @param packet
		 * @return 
		 * 
		 */			
		public static function createToggleSwitch(info:Object ,packet:PacketDecoder):ToggleSwitch
		{
			var ts:ToggleSwitch = new ToggleSwitch();
			ts.onText = "on";
			ts.offText = "off";
			
			//让滑道充满整个区域
			ts.trackLayoutMode = ToggleSwitch.TRACK_LAYOUT_MODE_SINGLE;
			
			ts.thumbFactory = function():Button
			{
				var button:Button = new Button();
				button.defaultSkin = packet.createScale9Image(info.thumb_default);
				button.downSkin = packet.createScale9Image(info.thumb_default);
				return button;
			};
			
			ts.labelFactory = function():ITextRenderer
			{
				var textRenderer:TextFieldTextRenderer = new TextFieldTextRenderer();
				var ttf:TextFormat = new TextFormat();
				ttf.font = info.textInfo.font;
				ttf.size = info.textInfo.size;
				ttf.align = info.textInfo.align;
				ttf.color = info.textInfo.color;
				ttf.underline = info.textInfo.underline;
				ttf.bold = info.textInfo.bold;
				ttf.italic = info.textInfo.italic;
				textRenderer.textFormat = ttf;
				return textRenderer;
			}
			
			ts.offTrackFactory = ts.onTrackFactory = function():Button
			{
				var button:Button = new Button();
				button.defaultSkin = packet.createScale9Image(info.background);
				return button;
			};
			
			return ts;
		}
		
		
		
		/**
		 *  
		 * @param info
		 * @param packet
		 * @return 
		 * 
		 */			
		public static function createCheck(info:Object ,packet:PacketDecoder):Check
		{
			var check:Check = new Check();
			
			check.defaultSkin = packet.createImage(info.thumb_default);
			check.defaultSelectedSkin = packet.createImage(info.thumb_select);
			check.minTouchWidth = check.minTouchHeight = check.defaultSkin.width * 1.5;
			
			/*check.label = "啥情况";
			check.labelOffsetX = 20;
			check.labelFactory = function():ITextRenderer
			{
				var textRenderer:TextFieldTextRenderer = new TextFieldTextRenderer();
				var ttf:TextFormat = new TextFormat();
				ttf.size = 30;
				ttf.color = 0xFFFFFF;
				textRenderer.textFormat = ttf;
				return textRenderer;
			}*/
			return check;
		}
		
		
		
		
		/**
		 *  
		 * @param info
		 * @param packet
		 * @return 
		 * 
		 */		
		public static function createRadio(info:Object ,packet:PacketDecoder):Radio
		{
			var radio:Radio = new Radio();
			radio.defaultSkin = packet.createImage(info.thumb_default);
			radio.defaultSelectedSkin = packet.createImage(info.thumb_select);
			radio.minTouchWidth = radio.minTouchHeight = radio.defaultSkin.width * 1.5;
			
			if(groups.hasOwnProperty(info.toggle_group)==false)
				groups[info.toggle_group] = new ToggleGroup();
			radio.toggleGroup = groups[info.toggle_group];
			
			return radio;
		}
		
		private static const groups:Object = new Object();
		
		
		
		
		
		
		
		
		/**
		 *  
		 * @param info
		 * @param packet
		 * @return 
		 * 
		 */			
		public static function createTabbar(info:Object ,packet:PacketDecoder):TabBar
		{
			var tab:TabBar = new TabBar();
			
			tab.tabFactory = function():Button
			{
				var btn:Button = new Button();
				btn.defaultSkin = packet.createScale9Image(info.thumb_default);
				btn.defaultSelectedSkin = packet.createScale9Image(info.thumb_select);
				btn.downSkin = packet.createScale9Image(info.thumb_down);
				
				btn.labelFactory = function():ITextRenderer
				{
					var textRenderer:TextFieldTextRenderer = new TextFieldTextRenderer();
					var ttf:TextFormat = new TextFormat();
					ttf.font = info.textInfo.font;
					ttf.size = info.textInfo.size;
					ttf.align = info.textInfo.align;
					ttf.color = info.textInfo.color;
					ttf.underline = info.textInfo.underline;
					ttf.bold = info.textInfo.bold;
					ttf.italic = info.textInfo.italic;
					textRenderer.textFormat = ttf;
					return textRenderer;
				};
				
				return btn;
			};
			return tab;
		}
		
		
		
		
		
		
		/**
		 *  
		 * @param info
		 * @param packet
		 * @return 
		 * 
		 */		
		public static function createSlider(info:Object ,packet:PacketDecoder):Slider
		{
			var slider:Slider = new Slider();
			slider.trackLayoutMode = Slider.TRACK_LAYOUT_MODE_SINGLE;
			
			slider.thumbProperties.defaultSkin = packet.createScale9Image(info.thumb_default);
			slider.thumbProperties.downSkin = packet.createScale9Image(info.thumb_down);
			
			slider.minimumTrackProperties.defaultSkin = packet.createScale9Image(info.background);
			slider.minimumTrackProperties.downSkin = packet.createScale9Image(info.background);
			slider.maximumTrackProperties.defaultSkin = packet.createScale9Image(info.background);
			slider.maximumTrackProperties.downSkin = packet.createScale9Image(info.background);
			
			return slider;
		}
		
		
		
		
		
		
		
		
		/**
		 * 
		 * @param info
		 * @param packet
		 * @return 
		 * 
		 */		
		public static function createProgressBar(info:Object ,packet:PacketDecoder):ProgressBar
		{
			var pb:ProgressBar = new ProgressBar();
			pb.backgroundSkin = packet.createScale9Image(info.background);
			pb.fillSkin = packet.createScale9Image(info.thumb_default);;
			return pb;
		}
		
		
		
		
		
		
		
		
		
		
		/**
		 *  
		 * @param normalSkinTexture
		 * @param selectedSkinTexture
		 * @return 
		 * 
		 */		
		public static function createPageIndicator(info:Object ,packet:PacketDecoder):PageIndicator
		{
			var pi:PageIndicator = new PageIndicator();
			pi.normalSymbolFactory = function():Image {return packet.createImage(info.thumb_default);}
			pi.selectedSymbolFactory = function():Image {return packet.createImage(info.thumb_select);}
			return pi;
		}
		
		
		
		
		
		/**
		 *  
		 * @param background
		 * @return 
		 * 
		 */	
		
		public static function createNumerIcStepper(
			background:Scale9Image ,
			defaultSkin:Scale9Image ,
			downSkin:Scale9Image
		):NumericStepper
		{
			var ns:NumericStepper = new NumericStepper();
			//ns.buttonLayoutMode = NumericStepper.BUTTON_LAYOUT_MODE_SPLIT_HORIZONTAL;
			
			ns.decrementButtonFactory = function():Button
			{
				var button:Button = new Button();
				//skin the decrement button here
				button.defaultSkin = defaultSkin;
				button.downSkin = downSkin;
				return button;
			};
			
			ns.incrementButtonLabel = "+";
			ns.decrementButtonLabel = "-";
			
			ns.textInputFactory = function():TextInput
			{
				var input:TextInput = new TextInput();
				//skin the text input here
				input.backgroundSkin = background;
				input.padding = 20;
				input.setSize(200 ,30);
				return input;
			}
				
			return ns;
		}
		
		
		
		
		
		
		
		/**
		 * 
		 * @param defaultSkin
		 * @param downSkin
		 * @param hoverSkin
		 * @param backgroundSkin
		 * @return 
		 * 
		 */		
		public static function createPickerList(
			defaultSkin:Scale9Image ,
			downSkin:Scale9Image,
			hoverSkin:Scale9Image ,
			backgroundSkin:Scale9Image
		):PickerList
		{
			var pl:PickerList = new PickerList();
			
			pl.listProperties.@itemRendererProperties.labelField = "text";
			pl.listProperties.@itemRendererProperties.iconSourceField = "thumbnail";
			pl.labelField = "text";
			pl.prompt = "Select an Item";
			pl.selectedIndex = -1;
			
			pl.buttonFactory = function():Button
			{
				var button:Button = new Button();
				button.defaultSkin = defaultSkin;
				button.downSkin = downSkin;
				button.hoverSkin = hoverSkin;
				return button;
			};
			
			pl.listFactory = function():List
			{
				var list:List = new List();
				list.backgroundSkin = backgroundSkin;
				return list;
			};
			
			return pl;
		}
		
		
		
		
		/**
		 *  
		 * @return 
		 * 
		 */		
		public static function createList(
			bitmapFont:BitmapFont,
			defaultSkin:Scale9Image
		):List
		{
			var list:List = new List();
			
			list.itemRendererFactory = function():IListItemRenderer
			{
				var renderer:DefaultListItemRenderer = new DefaultListItemRenderer();
				renderer.defaultSkin = defaultSkin;
				renderer.defaultLabelProperties.textFormat = new BitmapFontTextFormat(bitmapFont);
				return renderer;
			};
			
				
			list.horizontalScrollBarFactory = list.verticalScrollBarFactory = function():IScrollBar
			{
				var scrollBar:ScrollBar = new ScrollBar();
				scrollBar.direction = ScrollBar.DIRECTION_VERTICAL;
				scrollBar.trackLayoutMode = ScrollBar.TRACK_LAYOUT_MODE_SINGLE;
				scrollBar.thumbFactory = function():Button
				{
					var track1:Button = new Button();
					track1.defaultSkin =  new Quad(10 ,20 ,0xCCCCCC);
					track1.downSkin =  new Quad(10 ,20 ,0x999999);
					return track1;
				};
				
				return scrollBar;
			};
			
			return list;
		}
	}
}